package com.aaa.gs.service.impl;

import com.aaa.common.entity.GoodsImage;
import com.aaa.common.entity.User;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.GoodsImageVO;
import com.aaa.gs.dao.GoodsImageDao;
import com.aaa.gs.service.GoodsImageService;
import com.aaa.gs.service.RemoteUserService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * (GoodsImage)表服务实现类
 *
 * @author makejava
 * @since 2025-09-18 09:45:37
 */
@Service("goodsImageService")
public class GoodsImageServiceImpl implements GoodsImageService {
    @Resource
    private GoodsImageDao goodsImageDao;
    @Resource
    private RemoteUserService remoteUserService;
    @Resource
    private HttpServletRequest request;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GoodsImageVO queryById(Integer id) {
        return this.goodsImageDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<GoodsImageVO> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        GoodsImageVO goodsImage = page.getData();
        List<GoodsImageVO> goodsImageList = goodsImageDao.queryAllByLimit(goodsImage);
        for (GoodsImageVO image : goodsImageList) {
            if (Objects.nonNull(image.getCreator())){
                //设置创建人
               image.setUserB(remoteUserService.queryById(image.getCreator()));
            }else {
                image.setUserB(new User());
            }
        }
        //return new PageInfo(goodsImageDao.queryAllByLimit(goodsImage));
        return new PageInfo(goodsImageList);
    }
    @Override
    public PageInfo queryByPage1(Page<GoodsImageVO> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        GoodsImageVO goodsImageVO = page.getData();
        return new PageInfo(goodsImageDao.queryAllByLimit(goodsImageVO));
    }

    /**
     * 新增数据
     *
     * @param goodsImage 实例对象
     * @return 实例对象
     */
    @Override
    public GoodsImage insert(GoodsImage goodsImage) {
        String userId = TokenUtil.getUserId(request);
        goodsImage.setCreator(Integer.parseInt(userId));
        //goodsImage.setCreatetime(new Date());
        this.goodsImageDao.insert(goodsImage);
        return goodsImage;
    }

    /**
     * 修改数据
     *
     * @param goodsImage 实例对象
     * @return 实例对象
     */
    @Override
    public GoodsImageVO update(GoodsImageVO goodsImage) {
        this.goodsImageDao.update(goodsImage);
        return this.queryById(goodsImage.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.goodsImageDao.deleteById(id) > 0;
    }
}
